package martin.tpfinal.grabar.comunicacion;

import java.io.IOException;
import java.net.Socket;

import org.apache.log4j.Logger;

import martin.tpfinal.grabar.principal.Principal;
import martin.tpfinal.grabar.principal.PropertiesUtils;

public abstract class Conector {
	
    private static final Logger logger = Logger.getLogger(Conector.class);
    
    protected static String DESTINO;
    protected static String PUERTO;
    protected static String TIMEOUT;

    private int port;
    private String host;
    private boolean listening;

    
	public abstract String processAction(byte[] audioFile);

	protected int getTimeOut() {
	    return Integer.parseInt(PropertiesUtils.getProperties().getProperty(TIMEOUT));
	}

	public Socket getSocket() {
	        
	    Socket socket=null;
	    try {
	        logger.info("Obteniendo socket");
	        socket = new Socket(getHost(), getPort());
	        socket.setTcpNoDelay(true);
	        socket.setSoTimeout(getTimeOut());
	    } catch (IOException e) {
	        logger.error("Error getting socket: ", e);
	    }
	    return socket;
	}

    protected void close() {
        try {
            getSocket().close();
        } catch (IOException e) {
            
        }
    }

    public void setListening(boolean listening) {
        this.listening = listening;
    }


    public boolean isListening() {
        return listening;
    }


    public void setHost(String host) {
        this.host = host;
    }


    public String getHost() {
        return host;
    }


    public void setPort(int port) {
        this.port = port;
    }


    public int getPort() {
        return port;
    }

}
